package com.ay.selfservicemachine.webview;

import android.Manifest;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebStorage;
import android.webkit.WebView;


import com.ay.selfservicemachine.R;
import com.ay.selfservicemachine.base.BaseActivity;
import com.ay.selfservicemachine.databinding.ActivityWebBinding;
import com.ay.selfservicemachine.permission.PermissionCommander;
import com.ay.selfservicemachine.permission.PermissionListener;
import com.ay.selfservicemachine.utils.HQWPermissionUtil;

import java.util.Timer;
import java.util.TimerTask;


public class WebActivity extends BaseActivity<ActivityWebBinding, IWebView, WebPresenter> implements IWebView {

   public static WebActivityCallback webActivityCallback;

    @Override
    protected void initView() {
        super.initView();
//        mPresenter.registerReceiver();//注册广播
        mPresenter.setSetting(root.webview);
        mPresenter.clientListener(root.webview);
        mPresenter.initIdCardSdt();//初始话身份证读卡器


        root.tvHttpBack.setOnClickListener(v -> {
            root.webview.goBack();
        });
        root.tvHttpRetry.setOnClickListener(v -> {
            root.webview.reload();
        });
    }


    @Override
    protected ActivityWebBinding viewBinding() {
        return ActivityWebBinding.inflate(getLayoutInflater());
    }

    @Override
    protected WebPresenter createPresenter() {
        return new WebPresenter();
    }

    @Override
    protected int setFragmentContainerResId() {
        return 0;
    }

    @Override
    public void showHttpError(boolean isShow) {
        root.llHttpError.setVisibility(isShow ? View.VISIBLE : View.GONE);
        root.tvHttpBack.setVisibility(root.webview.canGoBack() ? View.VISIBLE : View.GONE);

    }

    @Override
    public void setTitle(String title) {
//        root.titlebar.setTitle(title, ITitleBarLayout.Position.MIDDLE);

    }

    @Override
    public void setUrl(String url) {
        root.webview.loadUrl(url);
    }

    @Override
    public void setLoadingProgress(int progress) {
        root.pbLoadingProgress.setProgress(progress);
        root.pbLoadingProgress.setMax(100);
        root.pbLoadingProgress.setVisibility(progress == 100 ? View.GONE : View.VISIBLE);
    }

    @Override
    public WebView getWebView() {
        return root.webview;
    }

    @Override
    public void checkAppVersion() {//检查更新
        PermissionCommander.startVerify(this, HQWPermissionUtil.PermissionDispose(new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
        }), true, getString(R.string.permission_content_file), getString(R.string.permission_content2_file), new PermissionListener() {
            @Override
            public void onsucceed() {
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(() -> {
                            mPresenter.startCheckAppVersion();
                        });
                    }
                }, 200);
            }

            @Override
            public void onfailure() {
                finish();
            }
        });
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && root.webview.canGoBack()) {
            root.webview.goBack();//返回上个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void finish() {
        super.finish();
//        if (webActivityCallback!=null&& !TextUtils.isEmpty(mPresenter.appid)){
//            webActivityCallback.close(mPresenter.appid);
//        }
    }

    @Override
    protected int statusNavigationBarType() {
        return 0;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        WebStorage.getInstance().deleteAllData();
        root.webview.clearCache(true);
        root.webview.clearHistory();
        root.webview.clearFormData();
        root.webview.clearSslPreferences();
        root.webview.clearMatches();
        deleteDatabase("webview.db");
        deleteDatabase("webviewCache.db");

//        mPresenter.unregisterReceiver();//取消注册广播接收器
    }

}
